package com.sgb.kjwl.view.ui.adapter.carrierDemand;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.sgb.kjwl.BR;
import com.sgb.kjwl.R;
import com.sgb.kjwl.model.entity.transport.CarrierListResultEntity;
import com.sgb.kjwl.view.ui.activity.transport.OrderBargainingActivity;
import com.swgk.core.dialog.RoundCornerDialog;
import com.swgk.core.recyclerview.adapter.BaseRecycleAdapter;
import com.swgk.core.recyclerview.adapter.ItemViewHolder;
import com.swgk.core.util.DateUtils;

import java.util.List;

public class CarrierDemandListAdapter extends BaseRecycleAdapter<CarrierListResultEntity> {

    public CarrierDemandListAdapter(Context context, List<CarrierListResultEntity> datas) {
        super(context, R.layout.item_carrier_demand, datas);
    }

    @Override
    public void convert(ItemViewHolder holder, CarrierListResultEntity carrierListResultEntity) {
        holder.setBinding(BR.viewModel, carrierListResultEntity);
        TextView tvTime = holder.itemView.findViewById(R.id.tv_time);
        if(!carrierListResultEntity.getInTime().equals("")) {
            tvTime.setText(DateUtils.stampToDate(String.valueOf(carrierListResultEntity.getInTime())).substring(0, 11) + "装货");
        }
        ImageView iv_phone = holder.itemView.findViewById(R.id.iv_phone);
        iv_phone.setOnClickListener(view -> showDialog(carrierListResultEntity.getContrctsNumber()));
        ImageView iv_yijia = holder.itemView.findViewById(R.id.iv_yijia);
        iv_yijia.setOnClickListener(view -> OrderBargainingActivity.start(context, OrderBargainingActivity.BARGAINING, carrierListResultEntity.getNo()));
        ImageView iv_xiadan = holder.itemView.findViewById(R.id.iv_xiadan);
        iv_xiadan.setOnClickListener(view -> OrderBargainingActivity.start(context, OrderBargainingActivity.ORDER, carrierListResultEntity.getNo()));

    }

    public void showDialog(String phone) {
        View view = View.inflate(context, R.layout.dialog_two_btn, null);
        final RoundCornerDialog roundCornerDialog = new RoundCornerDialog(context, 0, 0, view, R.style.RoundCornerDialog);
        roundCornerDialog.show();
        roundCornerDialog.setCanceledOnTouchOutside(false);// 设置点击屏幕Dialog不消失

        TextView tv_message = (TextView) view.findViewById(R.id.tv_message);
        TextView tv_title = view.findViewById(R.id.tv_title);
        TextView tv_logout_confirm = (TextView) view.findViewById(R.id.tv_logout_confirm);
        tv_title.setVisibility(View.VISIBLE);
        TextView tv_logout_cancel = (TextView) view.findViewById(R.id.tv_logout_cancel);
        if(TextUtils.isEmpty(phone)){
            tv_title.setText("温馨提示");
            tv_message.setText("认证后可拨打电话，去认证");
        }else{
            tv_title.setText("确认呼叫");
            tv_message.setText(phone);
        }

        //确定
        tv_logout_confirm.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if(TextUtils.isEmpty(phone)){
                    //去认证

                }else{
                    Intent intent = new Intent();
                    intent.setAction(Intent.ACTION_DIAL);
                    intent.setData(Uri.parse("tel:" + phone));
                    context.startActivity(intent);
                }

                roundCornerDialog.dismiss();
            }
        });

        //取消
        tv_logout_cancel.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                roundCornerDialog.dismiss();
            }
        });

    }
}
